using System.Collections.Generic;
using System.Linq;
using BCW;
using I2.Loc;
using UnityEngine;

public class MarketCarUpgradesInfoPanel : MarketProductInfoPanel
{
	public UISprite carTypeSprite;

	public UILabel carTypeLabel;

	public UISprite rareTypeSprite;

	public UILabel rareTypeLabel;

	public MarketProductButton bodyUp;

	public MarketProductButton engineUp;

	public MarketProductButton intakeUp;

	public MarketProductButton wheelsUp;

	public MarketVehicleSkinButton sampleButton;

	public UIGrid skinsGrid;

	public List<MarketVehicleSkinButton> skinsGO = new List<MarketVehicleSkinButton>();

	private MarketButton lastSelectedCar;

	private string selectedSubcategory;

	public override void SetInfo(Product product, bool setUpgrades, bool needComparison = true)
	{
		currentProduct = product;
		base.SetInfo(product, setUpgrades);
		ProductCar productCar = ((!needComparison) ? product : MarketControllerUI.instance.selectedProduct) as ProductCar;
		lastSelectedCar = MarketControllerUI.instance.productsPanel.selectedButton;
		selectedSubcategory = MarketControllerUI.instance.selectedSubcategory;
		carTypeSprite.gameObject.SetActive(true);
		InitUpgradeButton(bodyUp, product.id, UpgradeType.Body);
		InitUpgradeButton(engineUp, product.id, UpgradeType.Engine);
		InitUpgradeButton(intakeUp, product.id, UpgradeType.Intake);
		InitUpgradeButton(wheelsUp, product.id, UpgradeType.Wheels);
		InitSkins(productCar);
		carTypeSprite.spriteName = "CarProductType_" + productCar.carType;
		carTypeLabel.text = ScriptLocalization.Get("Market/CarType_" + productCar.carType);
		rareTypeSprite.spriteName = "RareType_" + productCar.rare;
		rareTypeLabel.text = ScriptLocalization.Get("Market/RareType_" + productCar.rare);
		SetTitleLocalization();
		SetDescriptionLocalization();
		if (product is ProductTitan)
		{
			carTypeSprite.gameObject.SetActive(false);
		}
		try
		{
			skinsGO.Find((MarketVehicleSkinButton x) => x.product.Equipped).OnClick();
		}
		catch
		{
			bodyUp.OnClick();
		}
	}

	private void InitUpgradeButton(MarketProductButton button, string carId, UpgradeType _type)
	{
		Product product = MarketController.Instance.GetFirstCarUpgrade(carId, _type);
		if (product != null)
		{
			while (product.nextProduct != null && product.nextProduct.Purchased)
			{
				product = product.nextProduct;
			}
			button.InitProductButton(MarketControllerUI.instance.productsPanel, product);
		}
		else
		{
			button.upgradesGrid.gameObject.SetActive(false);
			button.product = null;
			button.selectedBG.alpha = 0f;
			button.texture.LoadTextureAndAlphaMask("NGUI_Textures/", "Sign_Stop_Res");
			button.title.text = ScriptLocalization.Get("Market/No_upgrade");
		}
		button.gameObject.SetActive(true);
	}

	private void InitSkins(ProductCar productCar)
	{
		List<ProductVehicleSkin> source = MarketController.Instance.GetListSortProducts("Hidden", "VehicleSkins").Cast<ProductVehicleSkin>().ToList();
		List<ProductVehicleSkin> list = source.Where((ProductVehicleSkin x) => x.targetId == productCar.id).ToList();
		BCWUtils.BubbleSort(list, delegate(ProductVehicleSkin x, ProductVehicleSkin y)
		{
			if (x.defaultSkin)
			{
				return -1;
			}
			return y.defaultSkin ? 1 : x.id.CompareTo(y.id);
		});
		if (!(sampleButton == null) && !(skinsGrid == null))
		{
			skinsGO.ForEach(delegate(MarketVehicleSkinButton x)
			{
				Object.Destroy(x.gameObject);
			});
			skinsGO.Clear();
			for (int i = 0; i < list.Count; i++)
			{
				MarketVehicleSkinButton marketVehicleSkinButton = Object.Instantiate(sampleButton);
				marketVehicleSkinButton.transform.parent = skinsGrid.transform;
				marketVehicleSkinButton.gameObject.SetActive(true);
				marketVehicleSkinButton.InitProductButton(MarketControllerUI.instance.productsPanel, list[i]);
				marketVehicleSkinButton.name = i.ToString();
				skinsGO.Add(marketVehicleSkinButton);
			}
			UpdateSkins();
			skinsGrid.Reposition();
			skinsGrid.enabled = true;
		}
	}

	public void UpdateSkins()
	{
		skinsGO.ForEach(delegate(MarketVehicleSkinButton x)
		{
			x.checkSprite.gameObject.SetActive(x.product.Equipped);
		});
		skinsGO.ForEach(delegate(MarketVehicleSkinButton x)
		{
			x.SetBought(x.product.Purchased);
		});
		if (skinsGO.Count == 1)
		{
			skinsGO[0].gameObject.SetActive(false);
		}
	}

	public override void CloseInfoPanel()
	{
		base.CloseInfoPanel();
		if (lastSelectedCar != null && selectedSubcategory == MarketControllerUI.instance.selectedSubcategory)
		{
			lastSelectedCar.OnClick();
			MarketControllerUI.instance.productsPanel.selected = string.Empty;
			lastSelectedCar.LoadProductTexture((lastSelectedCar as MarketProductButton).product);
			lastSelectedCar = null;
		}
	}
}
